<?php
	class Database
	{
	    private static $_instance;
            private $_numRows;
            private $_rowsAffected;
            private $_resource;
            private $_connection;
            private $_connected;
            public $lastQuery;
		
	    private function __construct() {
	    	$this->_connected = false;
	    	$this->_connection = mysql_connect("localhost","miasi","EtH4eVBeZy9KWh64");
                if($this->_connection) {
                        $this->_connected = mysql_select_db("miasi",$this->_connection);
                }
                if(!$this->_connected) {
                        die("Could not connect to database. Error:" . mysql_error());
                }
	    }
	
	    public static function getInstance()
	    {
	        if (!self::$_instance) {
	            self::$_instance = new Database();
	        }
	        return self::$_instance;
	    }
		
            public function query($sql_statement) {
                    $this->lastQuery = $sql_statement;
                    $this->_resource = mysql_query($sql_statement);
                    
                    @$this->_rowsAffected = mysql_affected_rows($this->getLink());
                    @$this->_numRows = mysql_num_rows($this->_resource);
            }
		
		public function getAffectedRows() {
			return $this->_rowsAffected;
		}
		
		public function getNumRows() {
			return $this->_numRows;
		}
		
		public function getRowObject() {
			return mysql_fetch_object($this->_resource);
		}
		
		public function getObjectArray() {
			$arr = array();
			while($row = mysql_fetch_object($this->_resource)) {
				$arr[] = $row;
			}
			return $arr;
		}
		
		public function getField($fieldname) {
			$arr = array();
			for($i=0; $i<$this->_numRows; $i++) {
				$arr[] = mysql_result($this->_resource, $i, $fieldname);
			}
			return $arr;
		}
		
		public function insertId() {
			return mysql_insert_id();
		}
		
		public function getLink() {
			return $this->_connection;
		}
                
                public function getRowAssoc() {
                    return mysql_fetch_assoc($this->_resource);
                }
                
                public function getOne() {
                    return @reset(mysql_fetch_assoc($this->_resource));
                }
                
                public static function dbToObjectKeys($array) {
                    $ret = array();
                    foreach($array as $str => $value) {
                        if(stripos($str,'_') !== false) {
                            $str = explode('_',$str);
                            for($i=1;$i<count($str); $i++) {
                                $str[$i] = ucfirst($str[$i]);
                            }
                            $str = implode('',$str);
                        }
                        $ret[$str] = $value;
                    }
                    return $ret;
                }
                
	}  
?>